﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D; 

namespace IWebApp2
{
    public partial class Yehba : Form
    {
        public GraphicsPath oPath;
        private bool IsFormBeingDragged = false;
        private int MouseDownX;
        private int MouseDownY;
        private Thread myThread;
        public delegate void ListBoxCallback(String str,String str1);
        
        public Yehba()
        {
            InitializeComponent();


            oPath = new GraphicsPath();
            Point[] myArray =
             {
                 //new Point(0, 10),
                 //new Point(4, 4),
                 //new Point(10, 0),
                 //new Point(427, 0),
                 //new Point(433, 5),
                 //new Point(437,14),
                 new Point(0, 9),
                 new Point(1,7),
                 new Point(2,5),
                 new Point(4,3),
                 new Point(8, 1),
                 new Point(9,1),    //upper left corner
               
                 new Point(427,0),
                 new Point(430,2),
                 new Point(432,3),
                 new Point(433,5),
                 new Point(434,7),
                 new Point(436,10),
                 new Point(437,12),  //upper right corner
              
                 new Point(437,274),
                 new Point(436,277),
                 new Point(435,280),
                 new Point(433,283),
                 new Point(430,285),
                 new Point(427,287),
                 new Point(424,288),  //lower right corner
                 
                 
                 new Point(11,288),
                 new Point(9,287),
                 new Point(7,286),
                 new Point(4,284),
                 new Point(2,282),
                 new Point(1,280),
                 new Point(0,278),   //lower left corner




             };

            oPath.AddPolygon(myArray);
            this.Region = new Region(oPath);
            //this creates form with rounded corners
            ThreadStart threadStarter = new ThreadStart(ReceiveMessages);
            myThread = new Thread(threadStarter);
            myThread.IsBackground = true;
            myThread.Priority = ThreadPriority.Normal;
            myThread.Start();
           }

     
        private void Update_ListBox(String str,String str1)
        {
            this.listBox1.Items.Add(str+": "+str1);
        }

        private void ReceiveMessages()
        {
            while (true)
            {
               if (IWebApp2.IMForm.IsMessageAvailable==true)
                {
                    MessageInfo m = new MessageInfo();
                    m = IWebApp2.IMForm.offmessage.Dequeue();
                       if (m.from == this.User.Text)
                        {
                            listBox1.Invoke(new ListBoxCallback(Update_ListBox), new object[] { m.from, m.body });
                         
                        }
                }
                
               
            }
        }

     
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                //Thread.CurrentThread.Abort();
                myThread.Abort();
                this.Close();
                this.Dispose();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "--" + ex.StackTrace.ToString());
            }
        }

        private void Yehba_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = false;
            }
        }

        private void Yehba_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsFormBeingDragged = true;
                MouseDownX = e.X;
                MouseDownY = e.Y;
            }
        }

        private void Yehba_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsFormBeingDragged)
            {
                Point temp = new Point();
                temp.X = this.Location.X + (e.X - MouseDownX);
                temp.Y = this.Location.Y + (e.Y - MouseDownY);
                this.Location = temp;
                temp.X = 0;
                temp.Y = 0;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //WebService1.SendMessage(textBox1.Text,"639163821914@yb.yehba.com");
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
           // listBox1.Items.Add(WebService1.getMessage());
        }
 
    }
}
